package com.cn.ccp.action.course;

import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseSettingBean;
/**
 * 对课程的试题、练习、调查
 * @author Administrator
 *
 */
public class CourseSetAction extends BaseAction {
	private Logger log = Logger.getLogger(CourseSetAction.class);
	private String selectType = "0";
	private String courseId ;
	
	private List<CourseSettingBean> courseSetList ;
	
	private CourseSettingBean bean ;
	private String courseName ;
	
	private boolean showScore = false ;//当前进行编辑的时候,是否显示总分数项
	
	private boolean traningTest = false ;//是否是添加试题信息
	
	
	public String initCourseSetData(){
		selectType = "0";
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		courseId = varMap.get("id");
		getpagedetail();
		return "courseSet";
	}
	
	public String goAdd(){
		showScore = false ;
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		this.selectType = varMap.get("type");
		if(id != null && !id.equals("")){
			bean = getCourseService().getCourseSetingBean(id);
			if(bean.getType().equals("1"))
				showScore = true ;
			courseId = bean.getCourseId();
		}else{
			bean = new CourseSettingBean();
			this.courseId = varMap.get("courseId");
		}
		bean.setType(this.selectType);
		
		if(selectType.equals("1"))
			traningTest = true ;
		
		return "edit";
	}
	
	public String edit(){
//		if(bean.getType() == null || bean.getType().equals("") || bean.getType().equals("-1")){
//    		showMsg("type", "javax.faces.component.UIInput.REQUIRED");
//    		return "";
//    	}
		bean.setType(selectType);
		if(bean.getType().equals("1")){
			String reg ="\\d+\\.{0,1}\\d*";
			boolean isDigits = bean.getScore().matches(reg);
			if(!isDigits ||  bean.getScore().equals("0")){
				bean.setScore("0");
				showMsg("score", "course_page_course_submit_error_1_1");
				return "";
			}
		}
		
		boolean flag = false ;
		bean.setCourseId(courseId);
		if(bean.getId()== null || bean.getId().equals("") || bean.getId().equals("0")){
			flag = getCourseService().saveCourseSetting(bean);
		}else{
			flag = getCourseService().updateCourseSetting(bean);
		}
		if(flag)
			getpagedetail();
		else
			return "";
		
		if(bean.getActive().equals("1"))
			getCourseService().updateActiv(courseId, bean.getType());
		
		return "main";
	}
	
	
	public void selectTypeEvent(){
		getpagedetail();
	}
	
	public void scoreOnblur(){
		log.info("=----------bean.getType() = "+bean.getType());
		if(bean.getType().equals("1")){
			String reg ="\\d+\\.{0,1}\\d*";
			boolean isDigits = bean.getScore().matches(reg);
			if(!isDigits ||  bean.getScore().equals("0")){
				bean.setScore("0");
				showMsg("score", "course_page_course_submit_error_1_1");
			}else{
				showMsg("score", "");
			}
		}
	}
	
	
	public void activeChange(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String setID = varMap.get("id");
		String active = varMap.get("active");
		String type = varMap.get("selectType");
		courseId = varMap.get("courseId");
		getCourseService().updateActiv( courseId , type , setID ,  active);
		getpagedetail();
	}
	
	//编辑课程对应的考试/练习/调查详细信息
	public void editCourseSetInfo(){
		System.out.println("===============");
	}
	
	
	@Override
	public void getpagedetail() {
		try {
			setItemCount(getCourseService().getCourseSettingCount(courseId ,selectType));
			int currentpage = getCurrentPage();
			int pagesize = getPageSize();
			int start = (currentpage - 1) * pagesize;
			courseSetList = getCourseService().getCourseSetting(courseId , selectType ,start, pagesize);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getSelectType() {
		return selectType;
	}

	public void setSelectType(String selectType) {
		this.selectType = selectType;
	}


	public List<CourseSettingBean> getCourseSetList() {
		if(courseSetList == null)
			getpagedetail();
		return courseSetList;
	}


	public void setCourseSetList(List<CourseSettingBean> courseSetList) {
		this.courseSetList = courseSetList;
	}

	public String getCourseId() {
		return courseId;
	}

	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	public CourseSettingBean getBean() {
		if(bean == null)
			bean = new CourseSettingBean();
		return bean;
	}

	public void setBean(CourseSettingBean bean) {
		this.bean = bean;
	}

	public String getCourseName() {
		if(courseName == null  || courseName.equals("")){
			if(courseId != null  && !courseId.equals(""))
				courseName = getCourseService().getBeanById(courseId).getCourse_name();
		}
			
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	public boolean isShowScore() {
		return showScore;
	}

	public void setShowScore(boolean showScore) {
		this.showScore = showScore;
	}

	public boolean isTraningTest() {
		return traningTest;
	}

	public void setTraningTest(boolean traningTest) {
		this.traningTest = traningTest;
	}
	
	
}
